package com.example.sourcecodelearning.modules.designpattern.responsibilitychain;

import com.example.sourcecodelearning.modules.designpattern.responsibilitychain.handler.RequestHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 责任链模式
 *
 * @author yuzhenxin
 * @date 2023/8/1
 */
@Service
@Slf4j
public class ResponsibilityChain {

    private static List<RequestHandler> list;

    /**
     * 构造器注入
     *
     * @param requestHandlerList 实现了责任链的所有Bean
     */
    public ResponsibilityChain(List<RequestHandler> requestHandlerList) {
        list = requestHandlerList;
        log.info("初始化成功");
    }

    public void doFilter(Request request) {
        for (RequestHandler requestHandler : list) {
            requestHandler.handleRequest(request);
        }
    }
}

